package com.byf.goods.service.impl;

import com.byf.goods.GoodsApplication;
import com.byf.goods.domain.Goods;
import com.byf.goods.domain.GoodsVo;
import com.byf.goods.domain.req.GoodsReq;
import com.byf.goods.mapper.GoodsMapper;
import com.byf.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<GoodsReq> getGoods() {
        List<String> goodeNames = goodsMapper.getGoods();

        List<GoodsReq> goodsReqs = new ArrayList<>();
        goodeNames.forEach(goodeName->{
            List<Goods> list = goodsMapper.selectByName(goodeName);
            GoodsReq goodsReq = new GoodsReq();
            goodsReq.setGoodsName(goodeName);
            goodsReq.setTotalcount(list.size());
            List<GoodsVo> data=new ArrayList<>();
            list.forEach(goods -> {
                GoodsVo goodsVo = new GoodsVo();
                goodsVo.setMonth(goods.getMonth());
                goodsVo.setSalenum(goods.getSalenum());
                data.add(goodsVo);
            });
            goodsReq.setData(data);
            goodsReqs.add(goodsReq);
        });

        return goodsReqs;
    }
}
